/*
 *	Copyright (C) 2007-2009 Gabest
 *	http://www.gabest.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "stdafx.h"
#include "GS.h"
#include "GSdx.h"
#include "GSUtil.h"
#include "svnrev.h"
#include "xbyak/xbyak_util.h"

static struct GSUtilMaps
{
	uint8 PrimClassField[8];
	uint32 CompatibleBitsField[64][2];
	uint32 SharedBitsField[64][2];

	struct GSUtilMaps()
	{
		PrimClassField[GS_POINTLIST] = GS_POINT_CLASS;
		PrimClassField[GS_LINELIST] = GS_LINE_CLASS;
		PrimClassField[GS_LINESTRIP] = GS_LINE_CLASS;
		PrimClassField[GS_TRIANGLELIST] = GS_TRIANGLE_CLASS;
		PrimClassField[GS_TRIANGLESTRIP] = GS_TRIANGLE_CLASS;
		PrimClassField[GS_TRIANGLEFAN] = GS_TRIANGLE_CLASS;
		PrimClassField[GS_SPRITE] = GS_SPRITE_CLASS;
		PrimClassField[GS_INVALID] = GS_INVALID_CLASS;

		memset(CompatibleBitsField, 0, sizeof(CompatibleBitsField));

		for(int i = 0; i < 64; i++)
		{
			CompatibleBitsField[i][i >> 5] |= 1 << (i & 0x1f);
		}

		CompatibleBitsField[PSM_PSMCT32][PSM_PSMCT24 >> 5] |= 1 << (PSM_PSMCT24 & 0x1f);
		CompatibleBitsField[PSM_PSMCT24][PSM_PSMCT32 >> 5] |= 1 << (PSM_PSMCT32 & 0x1f);
		CompatibleBitsField[PSM_PSMCT16][PSM_PSMCT16S >> 5] |= 1 << (PSM_PSMCT16S & 0x1f);
		CompatibleBitsField[PSM_PSMCT16S][PSM_PSMCT16 >> 5] |= 1 << (PSM_PSMCT16 & 0x1f);
		CompatibleBitsField[PSM_PSMZ32][PSM_PSMZ24 >> 5] |= 1 << (PSM_PSMZ24 & 0x1f);
		CompatibleBitsField[PSM_PSMZ24][PSM_PSMZ32 >> 5] |= 1 << (PSM_PSMZ32 & 0x1f);
		CompatibleBitsField[PSM_PSMZ16][PSM_PSMZ16S >> 5] |= 1 << (PSM_PSMZ16S & 0x1f);
		CompatibleBitsField[PSM_PSMZ16S][PSM_PSMZ16 >> 5] |= 1 << (PSM_PSMZ16 & 0x1f);

		memset(SharedBitsField, 0, sizeof(SharedBitsField));

		SharedBitsField[PSM_PSMCT24][PSM_PSMT8H >> 5] |= 1 << (PSM_PSMT8H & 0x1f);
		SharedBitsField[PSM_PSMCT24][PSM_PSMT4HL >> 5] |= 1 << (PSM_PSMT4HL & 0x1f);
		SharedBitsField[PSM_PSMCT24][PSM_PSMT4HH >> 5] |= 1 << (PSM_PSMT4HH & 0x1f);
		SharedBitsField[PSM_PSMZ24][PSM_PSMT8H >> 5] |= 1 << (PSM_PSMT8H & 0x1f);
		SharedBitsField[PSM_PSMZ24][PSM_PSMT4HL >> 5] |= 1 << (PSM_PSMT4HL & 0x1f);
		SharedBitsField[PSM_PSMZ24][PSM_PSMT4HH >> 5] |= 1 << (PSM_PSMT4HH & 0x1f);
		SharedBitsField[PSM_PSMT8H][PSM_PSMCT24 >> 5] |= 1 << (PSM_PSMCT24 & 0x1f);
		SharedBitsField[PSM_PSMT8H][PSM_PSMZ24 >> 5] |= 1 << (PSM_PSMZ24 & 0x1f);
		SharedBitsField[PSM_PSMT4HL][PSM_PSMCT24 >> 5] |= 1 << (PSM_PSMCT24 & 0x1f);
		SharedBitsField[PSM_PSMT4HL][PSM_PSMZ24 >> 5] |= 1 << (PSM_PSMZ24 & 0x1f);
		SharedBitsField[PSM_PSMT4HL][PSM_PSMT4HH >> 5] |= 1 << (PSM_PSMT4HH & 0x1f);
		SharedBitsField[PSM_PSMT4HH][PSM_PSMCT24 >> 5] |= 1 << (PSM_PSMCT24 & 0x1f);
		SharedBitsField[PSM_PSMT4HH][PSM_PSMZ24 >> 5] |= 1 << (PSM_PSMZ24 & 0x1f);
		SharedBitsField[PSM_PSMT4HH][PSM_PSMT4HL >> 5] |= 1 << (PSM_PSMT4HL & 0x1f);
	}

} s_maps;

GS_PRIM_CLASS GSUtil::GetPrimClass(uint32 prim)
{
	return (GS_PRIM_CLASS)s_maps.PrimClassField[prim];
}

bool GSUtil::HasSharedBits(uint32 spsm, uint32 dpsm)
{
	return (s_maps.SharedBitsField[dpsm][spsm >> 5] & (1 << (spsm & 0x1f))) == 0;
}

bool GSUtil::HasSharedBits(uint32 sbp, uint32 spsm, uint32 dbp, uint32 dpsm)
{
	return ((sbp ^ dbp) | (s_maps.SharedBitsField[dpsm][spsm >> 5] & (1 << (spsm & 0x1f)))) == 0;
}

bool GSUtil::HasCompatibleBits(uint32 spsm, uint32 dpsm)
{
	return (s_maps.CompatibleBitsField[spsm][dpsm >> 5] & (1 << (dpsm & 0x1f))) != 0;
}

bool GSUtil::CheckDirectX()
{
	OSVERSIONINFOEX version;
	memset(&version, 0, sizeof(version));
	version.dwOSVersionInfoSize = sizeof(version);

	if(GetVersionEx((OSVERSIONINFO*)&version))
	{
		printf("Windows %d.%d.%d",
			version.dwMajorVersion,
			version.dwMinorVersion,
			version.dwBuildNumber);

		if(version.wServicePackMajor > 0)
		{
			printf(" (%s %d.%d)",
				version.szCSDVersion,
				version.wServicePackMajor,
				version.wServicePackMinor);
		}

		printf("\n");
	}

	if(IDirect3D9* d3d = Direct3DCreate9(D3D_SDK_VERSION))
	{
		D3DADAPTER_IDENTIFIER9 id;

		if(S_OK == d3d->GetAdapterIdentifier(D3DADAPTER_DEFAULT, 0, &id))
		{
			printf("%s (%d.%d.%d.%d)\n",
				id.Description,
				id.DriverVersion.HighPart >> 16,
				id.DriverVersion.HighPart & 0xffff,
				id.DriverVersion.LowPart >> 16,
				id.DriverVersion.LowPart & 0xffff);
		}

		d3d->Release();
	}

	string d3dx9_dll = format("d3dx9_%d.dll", D3DX_SDK_VERSION);

	if(HINSTANCE hDll = LoadLibrary(d3dx9_dll.c_str()))
	{
		FreeLibrary(hDll);
	}
	else
	{
		printf("Cannot find %s\n", d3dx9_dll.c_str());

		if(MessageBox(NULL, "You need to update some directx libraries, would you like to do it now?", "GSdx", MB_YESNO) == IDYES)
		{
			const char* url = "http://www.microsoft.com/downloads/details.aspx?FamilyId=2DA43D38-DB71-4C1B-BC6A-9B6652CD92A3";

			ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
		}

		return false;
	}

	//printf("test GSVector4(1, 2, 3, 4): %.3f", (float)GSVector4(1,2,3,4).GetValue());

#if _M_CUDA == 0x1

	int deviceCount = 0;
	cudaDeviceProp devProp;

	cudaGetDeviceCount ( &deviceCount );

	//CUDA_Mat cuMat;
	printf("MUL24 TEST: result(512*16(=8192)) = %d\n", cu_mul24(512, 16));
	printf("FMULRN TEST: result(2.5*2) = %.3f\n", cu_fmul_rn(2.5, 2));
	//printf("MULHI TEST: result(2000*4000) = %d\n", cu_mulhi(2000, 4000));

	if(deviceCount<=0)
	{
		string _s = format("Your GPU does not support CUDA!");
		MessageBox(GetActiveWindow(), _s.c_str(), "GSdx", MB_OK);
		return false;
	}

	printf ( "Found %d GPU CUDA devices\n", deviceCount );

	for ( int device = 0; device < deviceCount; ++device )
	{
        cudaGetDeviceProperties ( &devProp, device );

		printf ( "-------------------------------------------------\n" );
		printf ( "Device %d\n", device );
		printf ( "Compute capability      : %d.%d\n",	devProp.major, devProp.minor );
		printf ( "Name                    : %s\n",		devProp.name				 );
		printf ( "Clock Rate              : %d MHz\n",	devProp.clockRate/1000		 );
		printf ( "Multi Processor Count   : %d\n",		devProp.multiProcessorCount	 );
		printf ( "Total Global Memory     : %d\n",		devProp.totalGlobalMem		 );
		printf ( "Shared memory per block : %d\n",		devProp.sharedMemPerBlock	 );
		printf ( "Registers per block     : %d\n",		devProp.regsPerBlock		 );
		printf ( "Warp size               : %d\n",		devProp.warpSize			 );
		printf ( "Max threads per block   : %d\n",		devProp.maxThreadsPerBlock	 );
		printf ( "Total constant memory   : %d\n",		devProp.totalConstMem		 );
		printf ( "-------------------------------------------------\n\n" );
	}
#endif

	return true;
}

bool GSUtil::CheckSSE(bool showlog)
{
	Xbyak::util::Cpu cpu;
	Xbyak::util::Cpu::Type type;

	//_USE_SSE4A_ = Xbyak::util::Cpu::tSSE4a;
	_USE_SSE4A_ = !!theApp.GetConfig("UseSeeSSE4A", 0);

	if( _USE_SSE4A_ && showlog ) printf("Detect CPU support: SSE4A\n");

	type = Xbyak::util::Cpu::tSSE41;
	if(!cpu.has(type))
	{
		type = Xbyak::util::Cpu::tSSSE3;
		if(!cpu.has(type))
		{
			type = Xbyak::util::Cpu::tSSE3;
			if(!cpu.has(type))
			{
				type = Xbyak::util::Cpu::tSSE2;
				if(!cpu.has(type))
				{
					string s = format("This CPU does not support SSE2!");
					MessageBox(GetActiveWindow(), s.c_str(), "GSdx", MB_OK);
					_SSE_V = 0x100;
					return false;
				}
				else
				{
					_SSE_V = 0x200;
					if(!_USE_SSE4A_ && showlog) printf("Detect CPU support: SSE2\n");
				}
			}
			else
			{
				_SSE_V = 0x300;
				if(!_USE_SSE4A_ && showlog) printf("Detect CPU support: SSE3\n");
			}
		}
		else 
		{
			_SSE_V = 0x301;
			if(!_USE_SSE4A_ && showlog) printf("Detect CPU support: SSSE3\n");
		}
	}
	else 
	{
		_SSE_V = 0x401;
		if(!_USE_SSE4A_ && showlog) printf("Detect CPU support: SSE4\n");
	}

/*
	if(_USE_SSE4A_)
		printf("Set CPU instructions: SSE4A\n");
	else if (_SSE_V >= 0x401)
		printf("Set CPU instructions: SSE4\n");
	else if (_SSE_V >= 0x301)
		printf("Set CPU instructions: SSSE3\n");
	else if (_SSE_V >= 0x300)
		printf("Set CPU instructions: SSE3\n");
	else if (_SSE_V >= 0x200)
		printf("Set CPU instructions: SSE2\n");
	else
		printf("ERROR: This CPU does not support SSE2!\n");
*/
	return true;
}

typedef IDirect3D9* (WINAPI * LPDIRECT3DCREATE9) (UINT);

static HMODULE				s_hModD3D9 = NULL;
static LPDIRECT3DCREATE9	s_DynamicDirect3DCreate9 = NULL;


char* GSUtil::GetLibName()
{
	static string str;

	str = format("GSdx %d", SVN_REV);

	if(SVN_MODS) str += "m";

	#if _M_AMD64
	str += " 64-bit";
	#endif

	list<string> sl;

	// TODO: gcc

	#ifdef __INTEL_COMPILER
	sl.push_back(format("Intel C++ %d.%02d", __INTEL_COMPILER / 100, __INTEL_COMPILER % 100));
	#elif _MSC_VER
	sl.push_back(format("MSVC %d.%02d", _MSC_VER / 100, _MSC_VER % 100));
	#endif

	if(_SSE_V == 0x100) CheckSSE(false);

	if (_USE_SSE4A_)
	sl.push_back("SSE4A");
	else if (_SSE_V >= 0x401)
	sl.push_back("SSSE4");
	else if (_SSE_V >= 0x301)
	sl.push_back("SSSE3");
	else if (_SSE_V >= 0x300)
	sl.push_back("SSE3");
	else if (_SSE_V >= 0x200)
	sl.push_back("SSE2");
	else if (_SSE_V >= 0x100)
	sl.push_back("SSE");

	for(list<string>::iterator i = sl.begin(); i != sl.end(); )
	{
		if(i == sl.begin()) str += " (";
		str += *i;
		str += ++i != sl.end() ? ", " : ")";
	}

	return (char*)str.c_str();
}
